import PolylineTrailLinkMaterial from '../components/cesiums/plugins/PolylineTrailLinkMaterial';
import trackData from '../assets/data/track.json'
import dynamicData from '../assets/data/dynamic.json'
import * as cesium from 'cesium/Cesium'
const Cesium=cesium
class EntityGenerator {
    constructor(cesium,viewer) {
        this.Cesium = cesium;
        this.viewer=viewer;
        this.plugins = {};
        this.plugins.PolylineTrailLinkMaterial = new PolylineTrailLinkMaterial(cesium);
    }
    getPoints(data){
        let arr=[];
        for(let [index,item] of data.entries()){
            let newObj=index == data.length-1 ? item :data[index+1]
            arr.push({
                location:{x:item.x,y:item.y,z:item.z},
                start:item.datetime,
                end:newObj.datetime,
                endLocation:{x:newObj.x,y:newObj.y,z:newObj.z}
            })
        }
        return arr
    }
    //组织属性信息
    getAttrInfo(arr, points){

        for(let item of arr){
            let name=item.name;
            for(let temp of item.values){
                let result = points.find(el=>temp.datetime>=el.start && temp.datetime<el.end)
                result[name]=temp.value
            }
        }
    }

    getAttrInfoV2(attrs) {

        let dynamics = [];

        attrs.forEach(attr => {
            let data = [];

            for(let  [index, item] of attr.values.entries()){
                let newObj = index == attr.values.length-1 ? item :attr.values[index+1];
                data.push({
                    start: item.datetime,
                    end: newObj.datetime,
                    value: item.value
                });
            }
            dynamics.push({
                name: attr.name,
                data: data
            })
        });

        return dynamics;
    }

    /**
     * 将时空对象转换为Entity
     * @param {*} obj 时空对象
     */
    toEntity(obj) {

        let model = obj.forms.find(function(form){
            return form.type == 50;
        });
        
        let coord = JSON.parse(model.geom).coordinates;
        let points=[];
        let attrInfo={}
        let result=trackData.find(el=>el.oid == obj.id);
        let result2=dynamicData[0].data.find(el=>el.oid == obj.id);
        if(result){
            points=this.getPoints(result.data)
        }

        let dynamics = [];
        if(result2){
            this.getAttrInfo(result2.dynamic_values,points);
            dynamics = this.getAttrInfoV2(result2.dynamic_values);
        }

        return {
            id: obj.id,
            name: obj.name,
            position: new Cesium.Cartesian3.fromDegrees(
                coord[0],
                coord[1],
                10000
            ),
            model: {
                uri: "http://bt1.geosts.ac.cn/api/dae/model-service/model/rest/v0.1.0/datastore/slave/model/file/download/" + model.formref.refid,
                //minimumPixelSize: 128,
                //maximumScale: 20000
            },
            ref: obj,
            points:points,
            dynamics: dynamics,
            box: {
                show: true
            }
        }
    }

    /**
     * 根据对象生成关系连线（多个）
     * @param {*} obj 
     */
    toConnections(obj) {

        let connections = [];
        let _this = this;

        if(obj.network && obj.network.nodes){
            // 起终点对象
            let so = this.viewer.entities.getById(obj.id);
            let smodel = so.originalData.ref.forms.find(function(form){
                return form.type == 50;
            });
            let scoord = JSON.parse(smodel.geom).coordinates;

            obj.network.nodes.forEach(node => {
                let eo = this.viewer.entities.getById(node.relatedObjectId);
                // let emodel = eo.ref.forms.find(function(form){
                //     return form.type == 50;
                // });
                let emodel = eo.originalData.ref.forms.find(function(form){
                    return form.type == 50;
                });
                let ecoord = JSON.parse(emodel.geom).coordinates;

                if(so && eo){
                    connections.push({
                        polyline: {
                            positions: new _this.Cesium.CallbackProperty(function(time, result){
                                return [
                                    _this.Cesium.Cartesian3.fromDegrees(scoord[0], scoord[1], 10000),
                                    _this.Cesium.Cartesian3.fromDegrees(ecoord[0], ecoord[1], 10000),
                                ];
                            }, false),
                            width: 10,
                            material: new _this.Cesium.PolylineTrailLinkMaterialProperty(_this.Cesium.Color.RED, 20000),
                            // material:  Cesium.Color.RED.withAlpha(0.5),
                            ref: {
                                source: so.ref,
                                target: eo.ref,
                                relation: node.edge.relation
                            }
                        }
                    });
                }
            })
        }

        return connections;
    }
}

export default EntityGenerator;